﻿@using System.Linq;
@using OnWork.DataCenter.BLL.Standard;
<div class="row g-3 form-inline">
    <div class="col-2">
        <Select @bind-Value="@HttpRequest.Method" DisplayText="请求方法" ShowLabel="false" />
    </div>
    <div class="col-10">
      <BootstrapInput @bind-Value="@HttpRequest.URL" PlaceHolder="请输入http或https开头的地址" DisplayText="请求地址" ShowLabel="false" ShowLabelTooltip="true" IsTrim="true" />
    </div>
</div>
<Tab Height="@TabHeight" OnClickTabItemAsync="OnClickTabAsync">
    <TabItem Text="请求头">
        <Table TItem="HTTPRequestHeaderModel" UseInjectDataService="false" IsPagination="false" DoubleClickToEdit="true"
               IsStriped="true" IsBordered="true" ShowSkeleton="true" IsMultipleSelect="true" ShowEmpty="false" ShowLoading="true" ShowColumnList="true"
               ShowToolbar="true" ShowExtendButtons="true"
               ShowSearch="false" ShowAdvancedSearch="false"
               AutoGenerateColumns="false" EditMode="EditMode.InCell"
               OnSaveAsync="@OnHeadersSaveAsync" OnDeleteAsync="@OnHeadersDeleteAsync"
               ShowToastAfterSaveOrDeleteModel="false" Items="@HttpRequest.Headers">
            <TableColumns>
                <TableColumn Text="参数名称" @bind-Field="@context.Key" />
                <TableColumn Text="参数值" @bind-Field="@context.Value" />
            </TableColumns>
        </Table>
    </TabItem>
    <TabItem Text="查询参数">
        <Table TItem="HTTPRequestHeaderModel" UseInjectDataService="false" IsPagination="false" DoubleClickToEdit="true"
               IsStriped="true" IsBordered="true" ShowSkeleton="true" IsMultipleSelect="true" ShowEmpty="false" ShowLoading="true" ShowColumnList="true"
               ShowToolbar="true" ShowExtendButtons="true"
               ShowSearch="false" ShowAdvancedSearch="false"
               AutoGenerateColumns="false" EditMode="EditMode.InCell"
               OnSaveAsync="@OnQuerySaveAsync" OnDeleteAsync="@OnQueryDeleteAsync"
               ShowToastAfterSaveOrDeleteModel="false" Items="@HttpRequest.Query">
            <TableColumns>
                <TableColumn Text="参数名称" @bind-Field="@context.Key" />
                <TableColumn Text="参数值" @bind-Field="@context.Value" />
            </TableColumns>
        </Table>
    </TabItem>
    <TabItem Text="Body参数">
        <div>
            <RadioList ShowLabel="false" IsButton="true" @bind-Value="@HttpRequest.BodyType" OnValueChanged="OnBodyValueChangedAsync"></RadioList>
        </div>
        <div>
            <Tab @ref="BodyTabSet" Placement="Placement.Bottom" Height="@TabBodyHeight">
                <TabItem Text="1">
                    <h3>Body体为空</h3>
                </TabItem>
                <TabItem Text="2">
                    <Table TItem="HTTPRequestHeaderModel" UseInjectDataService="false" IsPagination="false"
                           IsStriped="true" IsBordered="true" ShowSkeleton="true" IsMultipleSelect="true" ShowEmpty="false" ShowLoading="true" ShowColumnList="true"
                           ShowToolbar="true" ShowExtendButtons="true"
                           ShowSearch="false" ShowAdvancedSearch="false"
                           AutoGenerateColumns="false" EditMode="EditMode.InCell"
                           OnSaveAsync="@OnBodyKeyValueSaveAsync" OnDeleteAsync="@OnBodyKeyValueDeleteAsync"
                           ShowToastAfterSaveOrDeleteModel="false" Items="@HttpRequest.BodyKeyValue">
                        <TableColumns>
                            <TableColumn Text="参数名称" @bind-Field="@context.Key" />
                            <TableColumn Text="参数值" @bind-Field="@context.Value" />
                        </TableColumns>
                    </Table>
                </TabItem>
                <TabItem Text="3">
                    <Textarea rows="15" placeholder="请输入文本" @bind-Value="@HttpRequest.BodyRaw"> </Textarea>
                </TabItem>
            </Tab>
        </div>
    </TabItem>
</Tab>

@code {

    /// <summary>
    /// Http请求
    /// </summary>
    [Parameter]
    public HttpRequestDto HttpRequest { get; set; } = new HttpRequestDto();

    /// <summary>
    /// 表格高度
    /// </summary>
    [Parameter]
    public int TabHeight
    {
        get => _TabHeight;
        set{
            _TabHeight = value;
            if (value>20)
            {
                TabBodyHeight = (value - 20);
            }
        }
    }
    private int _TabHeight;

    /// <summary>
    /// 表格高度
    /// </summary>
    public int TabBodyHeight { get; set; } = 0;

    /// <summary>
    /// 保存数据
    /// </summary>
    /// <param name="item"></param>
    /// <param name="changedType"></param>
    /// <returns></returns>
    private async Task<bool> OnHeadersSaveAsync(HTTPRequestHeaderModel item, ItemChangedType changedType)
    {
        if (changedType == ItemChangedType.Update) // 更新
        {
            var entry = HttpRequest.Headers.Where(x => x.ID == item.ID).FirstOrDefault();
            item.Adapt(entry);
            return await Task.FromResult(true);
        }
        else //新增
        {
            HttpRequest.Headers.Add(item);
            return await Task.FromResult(true);
        }
    }

    /// <summary>
    /// 删除数据
    /// </summary>
    /// <param name="items"></param>
    /// <returns></returns>
    private async Task<bool> OnHeadersDeleteAsync(IEnumerable<HTTPRequestHeaderModel> items)
    {
        HttpRequest.Headers.RemoveAll(x => items.Contains(x));
        return await Task.FromResult(true);
    }

    /// <summary>
    /// 保存数据
    /// </summary>
    /// <param name="item"></param>
    /// <param name="changedType"></param>
    /// <returns></returns>
    private async Task<bool> OnQuerySaveAsync(HTTPRequestHeaderModel item, ItemChangedType changedType)
    {
        if (changedType == ItemChangedType.Update) // 更新
        {
            var entry = HttpRequest.Query.Where(x => x.ID == item.ID).FirstOrDefault();
            item.Adapt(entry);
            return await Task.FromResult(true);
        }
        else //新增
        {
            HttpRequest.Query.Add(item);
            return await Task.FromResult(true);
        }
    }

    /// <summary>
    /// 删除数据
    /// </summary>
    /// <param name="items"></param>
    /// <returns></returns>
    private async Task<bool> OnQueryDeleteAsync(IEnumerable<HTTPRequestHeaderModel> items)
    {
        HttpRequest.Query.RemoveAll(x => items.Contains(x));
        return await Task.FromResult(true);
    }


    /// <summary>
    /// 保存数据
    /// </summary>
    /// <param name="item"></param>
    /// <param name="changedType"></param>
    /// <returns></returns>
    private async Task<bool> OnBodyKeyValueSaveAsync(HTTPRequestHeaderModel item, ItemChangedType changedType)
    {
        if (changedType == ItemChangedType.Update) // 更新
        {
            var entry = HttpRequest.BodyKeyValue.Where(x => x.ID == item.ID).FirstOrDefault();
            item.Adapt(entry);
            return await Task.FromResult(true);
        }
        else //新增
        {
            HttpRequest.BodyKeyValue.Add(item);
            return await Task.FromResult(true); 
        }
    }

    /// <summary>
    /// 删除数据
    /// </summary>
    /// <param name="items"></param>
    /// <returns></returns>
    private async Task<bool> OnBodyKeyValueDeleteAsync(IEnumerable<HTTPRequestHeaderModel> items)
    {
        HttpRequest.BodyKeyValue.RemoveAll(x => items.Contains(x));
        return await Task.FromResult(true);
    }

    private Tab? BodyTabSet { get; set; }

    private async Task OnBodyValueChangedAsync(HTTPRequestBodyType type)
    {
        switch (type)
        {
            case HTTPRequestBodyType.None:
                BodyTabSet?.ActiveTab(0);
                break;
            case HTTPRequestBodyType.FromData:
            case HTTPRequestBodyType.XWwwForm:
                BodyTabSet?.ActiveTab(1);
                break;
            default:
                BodyTabSet?.ActiveTab(2);
                break;
        }
        await Task.CompletedTask;
    }

    private async Task OnClickTabAsync(TabItem tabItem)
    {
        if (tabItem.Text == "Body参数")
        {
            await OnBodyValueChangedAsync(HttpRequest.BodyType);
        }
    }
}
